package com.scm.biz.service._impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.action.BaseAction;
import com.scm.biz.bean.BusinessFile;
import com.scm.biz.bean.DesignOrder;
import com.scm.biz.bean.Order;
import com.scm.biz.bean.OrderPattern;
import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.bean.ProofingOrder;
import com.scm.biz.bean.ProofingPattern;
import com.scm.biz.bean.ProofingPatternSize;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.BusinessFileDao;
import com.scm.biz.dao.CustomerDao;
import com.scm.biz.dao.DesignOrderDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderPatternDao;
import com.scm.biz.dao.ProofingOrderDao;
import com.scm.biz.dao.ProofingPatternDao;
import com.scm.biz.dao.ProofingPatternSizeDao;
import com.scm.biz.dao.SampleStoreDao;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.dto.ProofingOrderDto;
import com.scm.biz.dto.ProofingOrderNotice;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.query.ProofingOrderQuery;
import com.scm.biz.result.ProofingOrderResult;
import com.scm.biz.result.SampleStoreResult;
import com.scm.biz.service.BusinessConstantService;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.biz.service.ProofingOrderService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.Const;
import com.scm.utils.UfdmDateUtil;
import com.scm.utils.UploadAndDownUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 打样订单类的服务类的实现类
 * @author Young
 * @Date 2016.11.22
 */
@Service("proofingOrderService")
public class ProofingOrderServiceImpl implements ProofingOrderService{
	
	@Resource
	private ProofingOrderDao proofingOrderDao;
	@Resource
	private ProofingPatternDao proofingPatternDao;
	@Resource
	ProofingPatternSizeDao proofingPatternSizeDao;
	@Resource
	private OrderDao orderDao;
	@Resource
	private SampleStoreDao sampleStoreDao;
	@Resource
	private OrderPatternDao orderPatternDao;
	@Resource
	private BusinessFileDao businessFileDao;
	@Resource
	private BusinessConstantService businessConstantService;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;
	@Resource
	OrderTaskFlowService orderTaskFlowService;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	MsgNoticeService msgNoticeService;
	@Autowired
	DesignOrderDao designOrderDao;
	@Override
	public int insert(ProofingOrderDto proofingOrderDto) {
		 
		 ProofingOrder proofingOrder = new ProofingOrder();
			proofingOrder.setOrderId(proofingOrderDto.getOrderId());
			proofingOrder.setCreateAt(new Date());
			proofingOrder.setProductionEnddate(UfdmDateUtil.stringToDate(proofingOrderDto.getProductionEnddate(), Const.TIME_FORMAT));
			proofingOrder.setProofingEnddate(UfdmDateUtil.stringToDate(proofingOrderDto.getProofingEnddate(), Const.TIME_FORMAT));
			proofingOrder.setRequirement(proofingOrderDto.getRequirement());
			Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
			proofingOrder.setCreator(userId);
			proofingOrderDao.insert(proofingOrder);
			
			Map<String,List<ProofingPatternSize>> orderPatternIdMap = new HashMap<String, List<ProofingPatternSize>>();
			ProofingPatternSize proofingPatternSize = null;
			
			for(Map<String, Object> data:proofingOrderDto.getPatternSizeList()){
				proofingPatternSize = new ProofingPatternSize();
				if(data.get("color") !=null){
					proofingPatternSize.setColor(data.get("color").toString());
				}
				if(data.get("lAmount") !=null && !data.get("lAmount").toString().isEmpty()){
					proofingPatternSize.setlAmount(Integer.valueOf(data.get("lAmount").toString()));
				}
				if(data.get("mAmount") !=null && !data.get("mAmount").toString().isEmpty()){
					proofingPatternSize.setlAmount(Integer.valueOf(data.get("mAmount").toString()));
				}
				
				if(data.get("sAmount") !=null && !data.get("sAmount").toString().isEmpty()){
					proofingPatternSize.setsAmount(Integer.valueOf(data.get("sAmount").toString()));
				}
				if(data.get("xlAmount") !=null && !data.get("xlAmount").toString().isEmpty()){
					proofingPatternSize.setXlAmount(Integer.valueOf(data.get("xlAmount").toString()));
				}
				if(data.get("xsAmount") !=null && !data.get("xsAmount").toString().isEmpty()){
					proofingPatternSize.setXsAmount(Integer.valueOf(data.get("xsAmount").toString()));
				}
				if(data.get("xxlAmount") !=null && !data.get("xxlAmount").toString().isEmpty()){
					proofingPatternSize.setXxlAmount(Integer.valueOf(data.get("xxlAmount").toString()));
				}
				if(!orderPatternIdMap.containsKey(data.get("orderPatternId").toString())){
					orderPatternIdMap.put(data.get("orderPatternId").toString(), new ArrayList<ProofingPatternSize>());
				}
				orderPatternIdMap.get(data.get("orderPatternId").toString()).add(proofingPatternSize);
			}
			for(Entry<String,List<ProofingPatternSize>> entry:orderPatternIdMap.entrySet()){
				
				ProofingPattern proofingPattern = new ProofingPattern();
				proofingPattern.setOrderPatternId(Long.valueOf(entry.getKey()));
				proofingPattern.setProofingOrderId(proofingOrder.getId());
				proofingPattern.setStatus(OrderStatusEnum.PROOFING_CREATED.value());
				proofingPatternDao.insert(proofingPattern);
				
				OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
				orderTaskPerformer.setAction("F108");
				orderTaskPerformer.setBusinessId(proofingPattern.getId());
				orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
				orderTaskPerformer.setCreatedAt(new Date());
				orderTaskPerformer.setPerformer(userId);
				orderTaskPerformer.setUpdatedAt(new Date());
				orderTaskPerformer.setOrderId(proofingOrderDto.getOrderId());
				orderTaskPerformerService.addPerformer(orderTaskPerformer);
				
				//项目经理填写通知单
				orderTaskFlowService.addPerformer(OrderTypeEnum.T2,"F200", orderDao.selectByPrimaryKey(proofingOrder.getOrderId()).getManager(), proofingPattern.getId());
				//版型师
				orderTaskFlowService.addPerformer(OrderTypeEnum.T2,"F201", "R005", proofingPattern.getId());
				
				//面料师
				orderTaskFlowService.addPerformer(OrderTypeEnum.T2, "F210","R009", proofingPattern.getId());
				
				for(ProofingPatternSize _proofingPatternSize:entry.getValue()){
					_proofingPatternSize.setProofingPatternId(proofingPattern.getId());
					proofingPatternSizeDao.insert(_proofingPatternSize);
				}
			}
			
			
			
			
			
			Order order = orderDao.selectByPrimaryKey(proofingOrderDto.getOrderId());
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("设计稿已通过选款");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的设计稿已通过选稿，进入打样流程");
			msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
			msgNoticeDto.setTaskId(proofingOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setEnddate(proofingOrder.getProofingEnddate());
			msgNoticeDto.setOrderId(proofingOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,designOrderDao.selectByOrderId(proofingOrderDto.getOrderId()).getDesigner());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(proofingOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,order.getManager());
		
			
		 return 1;
	}

	@Override
	public List<ProofingOrderResult> selectByPage(
			ProofingOrderQuery proofingOrderQuery) {
		return proofingOrderDao.selectPage(proofingOrderQuery);
	}

	@Override
	public ProofingOrderResult selectById(Long id) {
		return proofingOrderDao.selectById(id);
	}

	@Override
	public Long save(ProofingOrderDto proofingOrderDto,HttpServletRequest req) throws Exception {
		Const.resetSubOrderCode();
		String customerId=req.getParameter("customerId");
		if(customerId==null||customerId.trim().isEmpty()){
			throw new UfdmException("用户为空");
		}
//		String managerId=req.getParameter("managerId");
//		if(managerId==null||managerId.trim().isEmpty()){
//			throw new UfdmException("项目经理为空");
//		}
		if(proofingOrderDto.getProofingEnddate()==null||proofingOrderDto.getProofingEnddate().trim().isEmpty()){
			throw new UfdmException("打样截止时间为空");
		}
		SimpleDateFormat sdf=new SimpleDateFormat(Const.TIME_FORMAT);
		Date proofingEnddate;
		try{
			proofingEnddate=sdf.parse(proofingOrderDto.getProofingEnddate());
		}catch(Exception e){
			e.printStackTrace();
			throw new UfdmException("打样截止时间有误");
		}
		Date productionEnddate;
		try{
			productionEnddate=sdf.parse(proofingOrderDto.getProofingEnddate());
		}catch(Exception e){
			productionEnddate=null;
		}
		//保存订单
		Order order=new Order();
		order.setCustomerId(new Long(customerId));
		order.setCreator((Integer)BaseAction.doGetSession("userId"));
		order.setCode(Const.getOrderCode());
		//order.setManager(Integer.valueOf(managerId));
		orderDao.insert(order);
		//保存打样订单
		ProofingOrder proofing=new ProofingOrder();
		proofing.setCreateAt(new Date());
		proofing.setOrderId(order.getId());
		proofing.setProofingEnddate(proofingEnddate);
		proofing.setProductionEnddate(productionEnddate);
		proofing.setRequirement(proofingOrderDto.getRequirement());
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
		proofing.setCreator(userId);
		proofingOrderDao.insert(proofing);
		String pattern=req.getParameter("pattern");
		savePattern(pattern,order,proofing.getId());
		
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("新的打样订单");
		msgNoticeDto.setContent(sysUser.getName()+"创建了"+customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的打样订单，请尽快安排项目经理，截止时间为"+UfdmDateUtil.dateToString(proofingEnddate, "MM月dd日"));
		msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
		msgNoticeDto.setTaskId(proofing.getId());
		msgNoticeDto.setEnddate(proofingEnddate);
		msgNoticeDto.setUserType(1);
		msgNoticeDto.setOrderId(proofing.getOrderId());
		msgNoticeService.insert(msgNoticeDto,"R007");

		return proofing.getId();
	}
	
	/**
	 * 保存订单款式和打样订单款式、打样订单款式尺码
	 * @param pattern
	 * @param orderId
	 * @param proofingOrderId
	 * @throws UfdmException 
	 */
	private void savePattern(String pattern,Order order,Long proofingOrderId) throws UfdmException {
		JSONArray arr=JSONArray.fromObject(pattern);
		for(Object object:arr){
			JSONObject obj=JSONObject.fromObject(object);
			if(obj.getInt("count")==0){
				continue;
			}
			Integer sampleId=obj.getInt("sampleId");
			SampleStoreResult sample=sampleStoreDao.selectById(sampleId);
			//保存订单款式
			String mainImg=UploadAndDownUtil.copyImage(sample.getMainImage(), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
			OrderPattern orderPattern=new OrderPattern();
			orderPattern.setCode(Const.getSubOrderCode(order.getCode()));
			orderPattern.setPatternNo(sample.getCode());
			orderPattern.setCreatedAt(new Date());
			orderPattern.setMainImage(mainImg);
			orderPattern.setSeason(sample.getSeason().substring(0,1));
			orderPattern.setMemo(sample.getMemo());
			orderPattern.setSex(sample.getSex());
			orderPattern.setOrderId(order.getId());
			orderPattern.setDetailDescription(Const.DETAIL_DESCRIPTION);
			orderPattern.setAccessoryDetail(Const.ACCESSORT_DETAIL);
			orderPatternDao.insert(orderPattern);
			//保存订单款式图片
			for(int i=0;i<sample.getImages().size();i++){
				BusinessFile file=new BusinessFile();
				if(!sample.getMainImage().equals(sample.getImages().get(i))){
					String img=UploadAndDownUtil.copyImage(sample.getImages().get(i), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
					file.setBusinessFile(img);
				}else{
					file.setBusinessFile(mainImg);
				}
				file.setCategory(FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
				file.setCreatedAt(new Date());
				file.setBusinessId(orderPattern.getId());
				businessFileDao.insert(file);
			}
			//保存订单款式风格
			for(int i=0;i<sample.getStyle().size();i++){
				businessConstantService.save(sample.getStyle().get(i), orderPattern.getId(), FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
			}
			//保存打样订单款式
			ProofingPattern proofingPattern=new ProofingPattern();

			proofingPattern.setProofingOrderId(proofingOrderId);
			proofingPattern.setOrderPatternId(orderPattern.getId());
			proofingPattern.setStatus(OrderStatusEnum.PROOFING_CREATED.value());//设置状态
			proofingPatternDao.insert(proofingPattern);
			
			Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
			
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F209");
			orderTaskPerformer.setBusinessId(proofingPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(userId);
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(order.getId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
			//供应链总监,分配项目经理
			orderTaskFlowService.addPerformer(OrderTypeEnum.T2,"F211", "R007", proofingPattern.getId());
			//版型师
			orderTaskFlowService.addPerformer(OrderTypeEnum.T2,"F201", "R005", proofingPattern.getId());
			
			//面料师
			orderTaskFlowService.addPerformer(OrderTypeEnum.T2, "F210","R009", proofingPattern.getId());
			//保存打样订单款式尺码

			JSONArray size=JSONArray.fromObject(obj.get("size"));
			for(Object o:size){
				JSONObject jsonObject=JSONObject.fromObject(o);
				if(jsonObject.getInt("count")==0){
					continue;
				}
				ProofingPatternSize proofingPatternSize=setProofingPatternSize(jsonObject);
				proofingPatternSize.setProofingPatternId(proofingPattern.getId());
				proofingPatternSizeDao.insert(proofingPatternSize);
			}

		}
	}

	@Override
	public void saveClose(
			Long proofingOrderId) {
		List<ProofingPattern> proofingPatternList = proofingPatternDao.selectListByOrderId(proofingOrderId);
		for(ProofingPattern proofingPattern:proofingPatternList){
			proofingPattern.setStatus(OrderStatusEnum.CLOSE.value());
			proofingPatternDao.updateByPrimaryKey(proofingPattern);
			
		}
		ProofingOrder proofingOrder = proofingOrderDao.selectByPrimaryKey(proofingOrderId);
		
		proofingOrderDao.updateByPrimaryKey(proofingOrder);
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		for(ProofingPattern proofingPattern :proofingPatternDao.selectByProofingOrderId(proofingOrderId)){
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F204");
			orderTaskPerformer.setBusinessId(proofingPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
		}
		
		/********************消息***************/
		Order order = orderDao.selectByPrimaryKey(proofingOrder.getOrderId());
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("样衣未通过选款");
		msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的样衣未通过选稿");
		msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
		msgNoticeDto.setTaskId(proofingOrder.getId());
		msgNoticeDto.setUserType(0);
		msgNoticeDto.setOrderId(order.getId());
		msgNoticeService.insert(msgNoticeDto,order.getManager());
		DesignOrder designOrder = designOrderDao.selectByOrderId(proofingOrder.getOrderId());
		msgNoticeDto.setOrderId(order.getId());
		msgNoticeService.insert(msgNoticeDto,designOrder.getDesigner());
	}

	@Override
	public void update(HttpServletRequest req,JSONArray arr) throws Exception {
		String ids=req.getParameter("ids");
		String[] id=ids.split(Const.SPLIT);
		String index=req.getParameter("index");
		if(index!=null){
			id=new String[]{id[new Integer(index)]};
		}
		String proofingOrderId=req.getParameter("proofingOrderId");
		SimpleDateFormat smf=new SimpleDateFormat(Const.TIME_FORMAT);
		for(int i=0;i<arr.size();i++){
			JSONObject obj=JSONObject.fromObject(arr.get(i));
			//保存订单款式
			OrderPattern orderPattern=new OrderPattern();
			orderPattern.setId(obj.getLong("orderPatternId"));
			if(!obj.containsKey("foldMethod")){
				throw new UfdmException("第"+(i+1)+"款折叠方式未选择");
			}
			orderPattern.setFoldMethodOther(obj.getString("foldMethod"));
			if(!obj.containsKey("packageMethod")){
				throw new UfdmException("第"+(i+1)+"款包装方式未选择");
			}
			orderPattern.setPackageMethodOther(obj.getString("packageMethod"));
			if(!obj.containsKey("checkStandard")){
				throw new UfdmException("第"+(i+1)+"款验货标准未选择");
			}
			orderPattern.setCheckStandardOther(obj.getString("checkStandard"));
			orderPattern.setDetailDescription(obj.getString("detailDescription"));
			orderPattern.setAccessoryDetail(obj.getString("accessoryDetail"));
			orderPattern.setUpdatedAt(new Date());
			orderPatternDao.updateByPrimaryKeySelective(orderPattern);
			//保存打样款式
			ProofingPattern proofingPattern=new ProofingPattern();
			proofingPattern.setId(Long.valueOf(id[i]));
			String deliveryDate=obj.getString("deliveryDate");
			try{
				proofingPattern.setDeliveryDate(smf.parse(deliveryDate));
			}catch(Exception e){
				e.printStackTrace();
				throw new UfdmException("第"+(i+1)+"款样衣工厂交期不符");
			}
			String factoryId=obj.getString("factoryId");
			if(factoryId==null||factoryId.trim().isEmpty()){
				throw new UfdmException("第"+(i+1)+"款未选择工厂");
			}
			ProofingOrder proofingOrder= proofingOrderDao.selectByPrimaryKey(Long.valueOf(proofingOrderId));
			
			if(proofingPattern.getFactoryId() ==null ||Integer.valueOf(factoryId) !=proofingPattern.getFactoryId()){
				/********************消息***************/
				SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
				MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
				msgNoticeDto.setTitle("工厂已安排，请上传尺码表");
				msgNoticeDto.setContent(sysUser.getName()+"已经安排了"+customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(proofingOrder.getOrderId()).getCustomerId().intValue()).getFullname()
										+ "客户的打样工厂，请尽快上传尺码表，截止时间为"+UfdmDateUtil.dateToString(proofingOrder.getProofingEnddate(), "MM月dd日"));
				msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
				msgNoticeDto.setTaskId(proofingOrder.getId());
				msgNoticeDto.setUserType(1);
				msgNoticeDto.setOrderId(proofingOrder.getOrderId());
				msgNoticeService.insert(msgNoticeDto,"R005");
			}
			proofingPattern.setFactoryId(Integer.valueOf(factoryId));
			proofingPattern.setId(new Long(id[i]));
			
			

			
			if(!obj.containsKey("batchMethod")){
				throw new UfdmException("第"+(i+1)+"款批版方式未选择");
			}
			proofingPattern.setBatchMethod(obj.getString("batchMethod"));
			proofingPattern.setDescription(obj.getString("description"));
			String testRequest=obj.getString("testRequest");
			if(testRequest==null||testRequest.trim().isEmpty()){
				throw new UfdmException("第"+(i+1)+"款测试要求为空");
			}
			proofingPattern.setTestRequest(testRequest);
			proofingPattern.setMemo(obj.getString("memo"));
			int status=proofingPatternDao.selectStatusById(proofingPattern.getId());
			proofingPattern.setStatus(status);
			//判断是否为创建订单状态
			if(proofingPattern.getStatus() <= OrderStatusEnum.PROOFING_ING.value()){
				SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
				OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
				orderTaskPerformer.setAction("F200");
				orderTaskPerformer.setBusinessId(proofingPattern.getId());
				orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
				orderTaskPerformer.setCreatedAt(new Date());
				orderTaskPerformer.setPerformer(sysUser.getId());
				orderTaskPerformer.setUpdatedAt(new Date());
				orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
				orderTaskPerformerService.addPerformer(orderTaskPerformer);
				
				proofingPattern.setStatus(OrderStatusEnum.PROOFING_ING.value());
				//如果已经完成所有的关联步骤，则进行下一步动作
				orderTaskFlowService.addPerformer(OrderTypeEnum.T2,"F202", "R004", proofingPattern.getId());
				
			}
		
			
			proofingPatternDao.updateByPrimaryKeySelective(proofingPattern);
			//删除款式尺码
			proofingPatternSizeDao.deleteByProofingPatternId(proofingPattern.getId().intValue());
			//保存款式尺码
			String patterns=obj.getString("patterns");
			JSONArray array=JSONArray.fromObject(patterns);
			for(Object o:array){
				JSONObject jsonObject=JSONObject.fromObject(o);
				if(jsonObject.getInt("count")==0){
					continue;
				}
				ProofingPatternSize proofingPatternSize=setProofingPatternSize(jsonObject);
				proofingPatternSize.setProofingPatternId(proofingPattern.getId());
				proofingPatternSizeDao.insert(proofingPatternSize);
			}
			
		}
		
		
	}
	
	/**
	 * 生成款式尺寸对象
	 * @param obj
	 * @return
	 * @throws UfdmException 
	 */
	private ProofingPatternSize setProofingPatternSize(JSONObject obj) throws UfdmException {
		ProofingPatternSize pattern=new ProofingPatternSize();
		String color=obj.getString("color");
		if(color==null||color.trim().isEmpty()){
			throw new UfdmException("颜色未填写");
		}
		pattern.setColor(color.trim());
    	if(!obj.getString("xs").trim().isEmpty()) pattern.setXsAmount(Integer.valueOf(obj.getString("xs").trim()));
    	if(!obj.getString("s").trim().isEmpty()) pattern.setsAmount(Integer.valueOf(obj.getString("s").trim()));
    	if(!obj.getString("m").trim().isEmpty()) pattern.setmAmount(Integer.valueOf(obj.getString("m").trim()));
    	if(!obj.getString("l").trim().isEmpty()) pattern.setlAmount(Integer.valueOf(obj.getString("l").trim()));
    	if(!obj.getString("xl").trim().isEmpty()) pattern.setXlAmount(Integer.valueOf(obj.getString("xl").trim()));
    	return pattern;
	}

	@Override
	public ProofingOrderNotice getByProofingPatternId(Long proofingPatternId) {
		return proofingOrderDao.selectByProofingPatternId(proofingPatternId);
	}

	@Override
	public int selectTotal(ProofingOrderQuery proofingOrderQuery) {
		return proofingOrderDao.selectTotal(proofingOrderQuery);
	}

	
}